home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Edit List and Spin / Transform / MatrixElements.cs next >
Encoding:
Text File  |  2001-01-15  |  4.0 KB  |  124 lines

  1. //---------------------------------------------
  2. // MatrixElements.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8.  
  9. class MatrixElements: Form
  10. {
  11.      Matrix          matrix; 
  12.      Button          btnUpdate;
  13.      NumericUpDown[] updown = new NumericUpDown[6];
  14.  
  15.      public event EventHandler Changed;
  16.  
  17.      public MatrixElements() 
  18.      {
  19.           Text = "Matrix Elements";
  20.           FormBorderStyle = FormBorderStyle.FixedDialog;
  21.           ControlBox      = false;
  22.           MinimizeBox     = false;
  23.           MaximizeBox     = false;
  24.           ShowInTaskbar   = false;
  25.  
  26.           String[] strLabel = { "X Scale:",     "Y Shear:",
  27.                                 "X Shear:",     "Y Scale:",
  28.                                 "X Translate:", "Y Translate:" };
  29.  
  30.           for (int i = 0; i < 6; i++)
  31.           {
  32.                Label label    = new Label();
  33.                label.Parent   = this;
  34.                label.Text     = strLabel[i];
  35.                label.Location = new Point(8, 8 + 16 * i);
  36.                label.Size     = new Size(64, 8);
  37.  
  38.                updown[i] = new NumericUpDown();
  39.                updown[i].Parent        = this;
  40.                updown[i].Location      = new Point(76, 8 + 16 * i);
  41.                updown[i].Size          = new Size(48, 12);
  42.                updown[i].TextAlign     = HorizontalAlignment.Right;
  43.                updown[i].ValueChanged += new EventHandler
  44.                                                   (UpDownOnValueChanged);
  45.                updown[i].DecimalPlaces = 2;
  46.                updown[i].Increment = 0.1m;
  47.                updown[i].Minimum = Decimal.MinValue;
  48.                updown[i].Maximum = Decimal.MaxValue;
  49.           }
  50.           btnUpdate          = new Button();
  51.           btnUpdate.Parent   = this;
  52.           btnUpdate.Text     = "Update";
  53.           btnUpdate.Location = new Point(8, 108);
  54.           btnUpdate.Size     = new Size (50, 16);
  55.           btnUpdate.Click   += new EventHandler(ButtonUpdateOnClick);
  56.  
  57.           AcceptButton = btnUpdate;
  58.  
  59.           Button btn   = new Button();
  60.           btn.Parent   = this;
  61.           btn.Text     = "Methods...";
  62.           btn.Location = new Point(76, 108);
  63.           btn.Size     = new Size(50, 16);
  64.           btn.Click   += new EventHandler(ButtonMethodsOnClick);
  65.  
  66.           ClientSize = new Size(134, 132);
  67.  
  68.           AutoScaleBaseSize = new Size(4, 8);
  69.      }
  70.      public Matrix Matrix 
  71.      {
  72.           get 
  73.           { 
  74.                matrix = new Matrix((float) updown[0].Value,
  75.                                    (float) updown[1].Value,
  76.                                    (float) updown[2].Value,
  77.                                    (float) updown[3].Value,
  78.                                    (float) updown[4].Value,
  79.                                    (float) updown[5].Value);
  80.                return matrix; 
  81.           } 
  82.           set 
  83.           { 
  84.                matrix = value;
  85.  
  86.                for (int i = 0; i < 6; i++)
  87.                     updown[i].Value = (decimal) value.Elements[i];
  88.           } 
  89.      }
  90.      void UpDownOnValueChanged(object obj, EventArgs ea)
  91.      {
  92.           Graphics grfx = CreateGraphics();
  93.  
  94.           bool boolEnableButton = true;
  95.  
  96.           try
  97.           {
  98.                grfx.Transform = Matrix;
  99.           }
  100.           catch
  101.           {
  102.                boolEnableButton = false;
  103.           }
  104.           btnUpdate.Enabled = boolEnableButton;          
  105.           grfx.Dispose();
  106.      }
  107.      void ButtonUpdateOnClick(object obj, EventArgs ea)
  108.      {
  109.           if (Changed != null)
  110.                Changed(this, new EventArgs());
  111.      }
  112.      void ButtonMethodsOnClick(object obj, EventArgs ea)
  113.      {
  114.           MatrixMethods dlg = new MatrixMethods();
  115.  
  116.           dlg.Matrix = Matrix;
  117.  
  118.           if (dlg.ShowDialog() == DialogResult.OK)
  119.           {
  120.                Matrix = dlg.Matrix;
  121.                btnUpdate.PerformClick();
  122.           }
  123.      }
  124. }